/**
 * Created by zhongb on 2017/4/11.
 */
var $ = require('jquery');
require('easyui');
var $alert = require('libs/alert');
$(function () {
    $(document).on('click','a[name=entDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'客户公司',true,true);
    });
    //获取url参数
    var id = $.getUrlParam('id'),
        type = $.getUrlParam('type'),
        typeTmpl = {
            '.': '获取异常',
            'C': '集货仓',
            'P': '港口仓',
            'O': '海外仓',
            'CP': '集货仓港口仓'
        };
    $.get('api/v1/p/mms/warehouse/load',{
        id: id
    },function (res) {
        if(res.code == 200){
            var data = res.data;
            var $firstTarget = $("#baseInfo").find("span");
            //元素赋值
            $firstTarget.eq(0).text(data.warehouseName);
            $firstTarget.eq(1).text(data.countryName);
            $firstTarget.eq(3).text(data.cityName);
            $firstTarget.eq(4).text(data.address);
            $firstTarget.eq(5).text(data.zipCode);
            $firstTarget.eq(6).text(data.timeZone);
            $firstTarget.eq(7).text(typeTmpl[data.type || '.']);
            $firstTarget.eq(8).text(data.createDt);
            $firstTarget.eq(9).text(data.linkMan);
            $firstTarget.eq(10).text(data.linkPhone);
            if(type === 'O'){
                $.get('api/v1/p/mms/warehouse/entlist',{
                    id: id,
                    type: 'O'
                },function (res) {
                    $('#ent_wrapper').show();
                    if(res.code == 200){
                        $('#mainTable').datagrid({
                            border : false,
                            data: res.data,
                            rownumbers:true,
                            emptyMsg: '暂无数据',
                            columns:[[
                                {
                                    field:'entName',
                                    title:'客户名称',
                                    width:'25%',
                                    formatter:function (value,row,index) {
                                        return "<a class='ellipsis' href='javascript:;' ref='company-detail?id="+row.entId+"' name='entDetail' title='"+value+"'>"+value+"</a>";
                                    }
                                },{
                                    field:'skuNum',
                                    title:'SKU数量',
                                    width:'15%'
                                },{
                                    field:'stockNum',
                                    title:'库存数量',
                                    width:'15%'
                                },{
                                    field:'inOrderNum',
                                    title:'累计入库订单',
                                    width:'15%'
                                },{
                                    field:'outOrderNum',
                                    title:'累计出库订单',
                                    width:'15%'
                                },{
                                    field:'refundNum',
                                    title:'累计退货订单',
                                    width:'15%'
                                }
                            ]],
                            onClickRow: function (rowIndex, rowData) {
                                $(this).datagrid('unselectRow', rowIndex);
                            }
                        });
                    }else{
                        $alert(res.message);
                    }
                })
            }
        }else{
            $alert(res.message);
        }
    });
});
